<?php

/**
 * @author aisyah
 * @copyright 2012
 */
session_start();
define('FPDF_FONTPATH', '../../library/fpdf/font/');
require_once('../../library/fpdf/fpdf.php');
require_once('../dao/database.php');
require_once('../dao/order_dao.php');
require_once('../../config/tanggal.php');

class PDF extends FPDF{
  function Header(){
	$string1="Cookies Land\n
		is All Bout Fullfill Ur Desire	
		";
	
	$string3="www.cookies-land.com\n";
	
    $this->Image('../../images/cookies-land.jpg',1,1,8);
    $this->SetTextColor(128,0,0);
    $this->SetFont('Arial','B','12');
	$this->MultiCell(19,0.2,$string1,0,'R');	
	$this->Ln();
	$this->SetFont('Arial','I','8');    
    $this->MultiCell(19,0.15,$string3,0,'R');
    $this->Ln();
	$this->Line(1,3.5,17,3.5);
    $this->Ln();
  }
  
  function Footer(){
    $this->SetY(-2,5);
    $this->Cell(0,1,'Cookies-Land@2012-Hak Cipta Dilindungi Undang-undang',0,0,'C');
  }
}

$tanggal = new Tanggal(date('Y-m-d'));
//ambil data order
   $db=new Database;    
       $no=1;
       $total = 0;
       $i=0;   
       
       foreach(array_keys($_SESSION['cart']) as $key){
        
            $db->show_Data('product_detail','*',null,null,'productdet_id='.$_SESSION['cart'][$key]['productid']);
       
            $hasil=$db->hasil;
            
            foreach($hasil as $data){
        
            $jml=$_SESSION['cart'][$key]['qty'];
            $harga=number_format($data['price'],2,',','.');
            $sub = number_format($jml*$data['price'],2,',','.');            
            
            $cell[$i][0]='#'.$no;
            $cell[$i][1]=$data['product_name'];
            $cell[$i][2]=$jml;
            $cell[$i][3]=$harga;
            $cell[$i][4]=$sub;            
               
       }
       
       $total += $_SESSION['cart'][$key]['prc'];
       
       $no++;
       $i++;
        
       }

//deklarasi FPDF
$pdf=new PDF('P','cm','A4');

$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('Arial','B','14');
$pdf->SetFillColor(255,255,255);
$pdf->Cell(19,1.5,'Surat Order Pelanggan',0,1,'C',1);

//pelanggan belum di sesuaikan dengan login-buat test saja
$pelanggan="Paijo";
//alamat kirim
$alamat = array();
$alamat['name'] = $_SESSION['alamat']['name'];
$alamat['street'] = $_SESSION['alamat']['street'];
$alamat['kecamatan'] = $_SESSION['alamat']['kecamatan'];
$alamat['city'] = $_SESSION['alamat']['city'];
$alamat['province'] = $_SESSION['alamat']['province'];
$alamat['zip_code'] = $_SESSION['alamat']['zip_code'];

$db->hasil="";

$db->show_Data('t_city','city_name',null,null,'city_id='.$alamat['city']);
$city = $db->hasil;
//print_r($city);
foreach($city as $hasil){
    $alamat['city'] = $hasil['city_name'];
}

$db->hasil="";
$db->show_Data('t_province','name',null,null,'province_id='.$alamat['province']);
$prov = $db->hasil;
//print_r($prov);
foreach($prov as $hasil){
    $alamat['province'] = $hasil['name'];
}
//$alamat= implode('</br>',$alamat);
$alamat = array($alamat['name'],$alamat['street'],$alamat['kecamatan'].', '.$alamat['city'],$alamat['province'].', '.$alamat['zip_code']);        
$alamatkirim=implode("\n",$alamat);

$order = new Order_Dao($db);

$nopo = $order->po_number();

$tglpo = $tanggal->get_tgl_indo();
//kayake gk perlu pake multicell
$pdf->SetFont('Arial','B','9');
$pdf->SetFillColor(192,192,192);
$pdf->Cell(9.5,0.5,'Pelanggan : ',0,0,'L',1);
$pdf->Cell(9.5,0.5,'Informasi PO : ',0,1,'L',1);
//$pdf->MultiCell(8,0.5,$pelanggan,0,'L');
$pdf->Cell(9.5,0.5,$pelanggan,0,0,'RLTB',0);
$pdf->Cell(9.5,0.5,"Nomor PO\t:".$nopo,0,1,'RLTB',0);
$pdf->Cell(9.5,0.5,"Jln Merak",0,0,'RLTB',0);
$pdf->Cell(9.5,0.5,"Tanggal PO\t: ".$tglpo,0,1,'RLTB',0);
$pdf->Ln();
$pdf->Cell(9.5,0.5,'Alamat Pengiriman : ',0,0,'L',1);
$pdf->Cell(9.5,0.5,'',0,1,'L',1);
$pdf->MultiCell(9.5,0.5,$alamatkirim,0,'L');
$pdf->Ln();
$pdf->Ln();

$pdf->SetFont('Arial','B','9');
$pdf->SetFillColor(192,192,192);
$pdf->Cell(1,1,'No',RLTB,0,'C',1);
$pdf->Cell(9,1,'Nama Produk',RLTB,0,'C',1);
$pdf->Cell(2,1,'Jumlah',RLTB,0,'C',1);
$pdf->Cell(3,1,'Harga Satuan (Rp.)',RLTB,0,'C',1);
$pdf->Cell(3,1,'Sub Total (Rp.)',RLTB,0,'C',1);

$pdf->Ln();
$pdf->SetFont('Arial','','8');
//mengisi data
for ($j=0;$j<$i;$j++){
    
  $pdf->Cell(1,0.8,$cell[$j][0],RLTB,0,'C');   //No      
  $pdf->Cell(9,0.8,$cell[$j][1],RLTB,0,'L'); // Nama Produk
  $pdf->Cell(2,0.8,$cell[$j][2],RLTB,0,'C'); // Jumlah
  $pdf->Cell(3,0.8,$cell[$j][3],RLTB,0,'R'); // Harga Satuan
  $pdf->Cell(3,0.8,$cell[$j][4],RLTB,0,'R'); // Sub Total  
  $pdf->Ln();
}
$pdf->Cell(15,0.8,"Biaya Kirim",RLTB,0,'R');
$pdf->Cell(3,0.8,number_format($_SESSION['biaya_kirim'],2,',','.'),RLTB,0,'R');
$pdf->Ln();
$pdf->Cell(15,0.8,"Total",RLTB,0,'R');
$pdf->Cell(3,0.8,number_format($total+$_SESSION['biaya_kirim'],2,',','.'),RLTB,0,'R');
$pdf->Ln();
$pdf->Cell(2,0.8,"Penting :",0,0,'L');
$pdf->Cell(8,0.8,"Klaim pembelian hanya dilayani 2 hari setelah barang diterima",0,0,'L');
$pdf->Ln();
//$pdf->Ln();

$pdf->Cell(9.5,0.8,"",0,0,'R');
$pdf->Cell(9.5,0.8,"Hormat Kami,",0,0,'C');
$pdf->Ln();
$pdf->Cell(9.5,0.8,"",0,0,'R');
$pdf->Cell(9.5,0.8,"Manajemen Cookies-Land",0,0,'C');

$pdf->Ln();
$pdf->Ln();
$pdf->Cell(9.5,0.8,"Kepuasan Anda Kebanggaan Bagi Kami",1,1,'L');

$pdf->Output();
  

?>